star.cutoffs = c(.1, .05, .01, .001))
writeLines(out,con = paste0('../results/E2_',(Sys.time() %>% str_replace_all(c(":" = "-", " " = "_"))),'.tex'))
rm(out, lm3)
#Table E4
out = stargazer(lm4,
covariate.labels = c('Asset Score', 'Jaipur Dummy','Patna Dummy'),
dep.var.labels = c(""),
column.labels = c('Proportion Sch. Caste and Sch. Tribes'),
add.lines = list(c('City','All')),
title = 'Caste and assets',
omit.stat = c('f','ser'),
label = 'Caste_Assets',
star.char = c(".", "*", "**", "***"),
star.cutoffs = c(.1, .05, .01, .001))
writeLines(out,con = paste0('../results/E4_',(Sys.time() %>% str_replace_all(c(":" = "-", " " = "_"))),'.tex'))
rm(out, lm4)
#Table E6
lm1 = lm('PartyFrac ~ Fractionalization + City', data = Neigh)
out = stargazer(lm1,
covariate.labels = c('Leader Frac.', 'Jaipur Dummy','Patna Dummy'),
dep.var.labels = c(""),
column.labels = c('Party Fractionalization'),
add.lines = list(c('City','All')),
title = 'Leadership Fractionalization and Party Fractionalization',
omit.stat = c('f','ser'),
label = 'Leader_Party_Frac',
star.char = c(".", "*", "**", "***"),
star.cutoffs = c(.1, .05, .01, .001))
writeLines(out,con = paste0('../results/E6_',(Sys.time() %>% str_replace_all(c(":" = "-", " " = "_"))),'.tex'))
rm(out, lm1)
#Table E5
lm1=(lm('El ~ NeighDens + AssetSum + City + PropSC + PropMuslim', data = Neigh))
lm2=(lm('PrimSch ~ NeighDens + AssetSum + City + PropSC + PropMuslim', data = Neigh))
lm3=(lm('NoTanker ~ NeighDens + AssetSum + City + PropSC + PropMuslim', data = Neigh))
lm4=(lm('Toilet ~ NeighDens + AssetSum + City + PropSC + PropMuslim', data = Neigh))
lm5=(lm('Recog ~ NeighDens + AssetSum + City + PropSC + PropMuslim', data = Neigh))
out = stargazer(lm1, lm2, lm3, lm4, lm5,
covariate.labels = c('Social Density','Asset Score','Jaipur Dummy','Patna Dummy','Low Caste','Muslim','Constant'),
#dep.var.labels = c('Elec.','School','Water','Toilet','Recog.'), header = F,
dep.var.labels = c(""), #header = F,
column.labels = c('Electr.','School','Water','Toilet','Recog.'), header = F,
add.lines = list(c('City','All','All','All','All','All')),
title = 'Neighborhood services and social density',
#no.space = T,
omit.stat = c('f','ser'),
label = 'Dens_Svcs_CasteMuslim',
star.char = c(".", "*", "**", "***"),
star.cutoffs = c(.1, .05, .01, .001))
writeLines(out,con = paste0('../results/E5_',(Sys.time() %>% str_replace_all(c(":" = "-", " " = "_"))),'.tex'))
rm(out, lm1, lm2, lm3, lm4, lm5)
# #What is magnitude of effect?
# sd(Neigh$NeighDens) #0.4708526 #IV
# sd(Neigh$El) #0.2902129 #DV
# sd(Neigh$PrimSch) #0.5076656 #DV
# sd(Neigh$NoTanker) #0.1565287 #DV
# sd(Neigh$Toilet) #0.2068612 #DV
# sd(Neigh$Recog) #0.2750397 #DV
# 0.102 * (0.4708526/ 0.2902129) # = 0.1654887  #Coef * (IV-SE/DV-SE): changing the IV by this many SE's changes the DV by this many SE's
# 0.367 * (0.4708526/ 0.5076656) # = 0.3403873  #Coef * (IV-SE/DV-SE): changing the IV by this many SE's changes the DV by this many SE's
# 0.064 * (0.4708526/ 0.1565287) # = 0.1925178  #Coef * (IV-SE/DV-SE): changing the IV by this many SE's changes the DV by this many SE's
# 0.079 * (0.4708526/ 0.2068612) # = 0.1798179  #Coef * (IV-SE/DV-SE): changing the IV by this many SE's changes the DV by this many SE's
# 0.184 * (0.4708526/ 0.2750397) # = 0.3149977  #Coef * (IV-SE/DV-SE): changing the IV by this many SE's changes the DV by this many SE's
###################################################################################################
##########################################################################################
#T1
s(A$Wave)
s(A$City)
s(Neigh$Wave)
s(Neigh$City)
##########################################################################################
library(plyr);library(dplyr)
library(tidyr)
library(ggplot2)
library(lmtest); library(multiwayvcov)#for clustered se's
library(stargazer)
library(stringr)
rm(list=ls())
Num = function(x){as.numeric(as.character(x))}
s = function(x){summary(factor(x))}
#set working directory
setwd('/Users/jeremyspater/Dropbox/duke/wibbels RA/Social_Density_and_Clientelism_JS-EW/Data and Code/jop replication files/scripts/')
A = read.csv('../data/6-27-17_supermerge_wleaderfrac_deid_trimmed.csv', sep = ';')
source('repl_6-9-17 cleaning and pca.R') #pca and variable cleaning. Table B1, Figure B1
source('6-14-17 alt pca.R') #alternate version of pca
source('18aug24 neighborhood.R') #add neigh soc dens to main dataframe; summary stats
source('repl_2-07-20_histo.R') #histograms (figures D1, D2)
source('18aug24 baltab.R') #balance table (C1)
source('7sep24 basic list.R') # list experiment (Table 3)
source('repl_6-14-17 inde conn list_11-5-18.R') #main result for individual clientelism (table 4, table F1)
source('repl_6-14-17 marginal effects.R') #marginal effects plot (A1)
source('6-27-17 neigh other list.R') #**Make Neigh dataframe (caste composition etc).
source('28aug24 inde conn list_plus.R') #Table G1
source('replication_networkgraphs_17aug2024.R') #Figure 2
#TABLE 2
#summary(factor(A$ListOut))
#0    1    2    3    4
#588 3078 2689  735  146
#summary(factor(A$ListOut[which(A$ListControl == T)]))
#0    1    2    3
#202 1085  844  273
#summary(factor(A$ListOut[which(A$ListFavor == T)]))
#0    1    2    3    4
#202 1010  942  232   72
#sum(A$ListControl == T) #2404
#sum(A$ListFavor == T) #2458
#check ceiling/floor
#(273 / sum(A$ListControl == T)) # 0.1135607 proportion that might be affected by ceiling
#202 / sum(A$ListControl == T) # 0.08402662 proportion that might be affected by ceiling
################################################################################################################
rm(list=ls())
library(plyr);library(dplyr)
library(tidyr)
library(igraph)
library(stringr)
setwd('/Users/jeremyspater/Dropbox/duke/wibbels RA/Social_Density_and_Clientelism_JS-EW/Data and Code/jop replication files/scripts')
dat = readRDS('/Users/jeremyspater/Dropbox/duke/wibbels RA/segregation paper/network/3-6-17 jaipat deid.RDS')
#########################################
#Social
GrSoc = function(x){
el_bor = cbind(x$code3,as.character(x$G.G2.G2B_Code2))
el_bor2 = cbind(x$code3,as.character(x$G.G3.G3B_Code2))
el_vis = cbind(x$code3,as.character(x$I.I7.I7B_Code2))
el = rbind(el_bor, el_bor2, el_vis)
el = el[complete.cases(el),]
gr = graph_from_edgelist(el,directed=TRUE) %>% simplify(remove.multiple=FALSE, remove.loops=TRUE)
V(gr)$Religion=as.character(dat$C.C6[match(V(gr)$name,dat$code3)])
V(gr)$Neighborhood = paste(x$City[1],x$A.A7[1])
return(gr)
}
#Political
GrPol = function(x){
el_ser = cbind(x$code3,as.character(x$I.I3_Group.I3B_Code2)) #help getting service
el_meet = cbind(x$code3,as.character(x$I.I16.I16_Lead.meeting)) #
el_vote = cbind(x$code3,as.character(x$I.I33B_Code2)) #important in how you vote
el_lea = cbind(x$code3,as.character(x$I.I25B_Code2)) #leader
el = rbind(el_ser, el_vote, el_lea)
el = el[complete.cases(el),]
gr = graph_from_edgelist(el,directed=TRUE)
V(gr)$Religion=as.character(dat$C.C6[match(V(gr)$name,dat$code3)])
V(gr)$Neighborhood = paste(x$City[1],x$A.A7[1])
return(gr)
}
#Economic
GrEcon = function(x){
el_job = cbind(x$code3,as.character(x$D.D4.D4B_Code2)) #help getting job
el_work = cbind(x$code3,as.character(x$D.D8.D8B_Code2)) #work with
el_unemp = cbind(x$code3,as.character(x$E.E3B_Code2)) #unable to find work
el_house = cbind(x$code3,as.character(x$H.H7.H7B_Code2)) #sell house
el = rbind(el_job, el_work, el_unemp, el_house)
el = el[complete.cases(el),]
gr = graph_from_edgelist(el,directed=TRUE)
V(gr)$Religion=as.character(dat$C.C6[match(V(gr)$name,dat$code3)])
V(gr)$Neighborhood = paste(x$City[1],x$A.A7[1])
return(gr)
}
#create "graphs" for each set of questions
SocList = dlply(dat, 'A.A7', function(x) GrSoc(x))
PolList = dlply(dat, 'A.A7', function(x) GrPol(x))
EconList = dlply(dat, 'A.A7', function(x) GrEcon(x))
#########################################################################################################
#plotting
Pl3 = function(x){
plot.igraph(x,vertex.size=5,vertex.label = NA,edge.arrow.size=0.1, edge.width=0.3, edge.lty=1,
margin = 0.0 , edge.color = 'black', vertex.color = 'gray')
}
PlF3 = function(y){#,d){
layout(matrix(c(1,2,3,4,5,6,7,8,9), nrow = 3, ncol = 3))
par(mar = c(0.1,0.1,0.1,0.1))
l_ply(y, Pl3)
}
png(filename=paste0('../results/F1_',(Sys.time() %>% str_replace_all(c(":" = "-", " " = "_"))),'.png'))
PlF3(SocList)
dev.off()
rm(list=ls())
library(plyr);library(dplyr)
library(tidyr)
library(igraph)
library(ggplot2)
library(stargazer)
setwd('/Users/jeremyspater/Dropbox/duke/wibbels RA/Social_Density_and_Clientelism_JS-EW/Data and Code/jop replication files/scripts')
dat = readRDS('/Users/jeremyspater/Dropbox/duke/wibbels RA/segregation paper/network/3-6-17 jaipat deid.RDS')
#put caste in own category
dat$C.C8_Caste = mapvalues(dat$C.C8_Caste, from = c('BC', 'General', 'OBC', 'SC', 'ST'),
to = c('General/BC/OBC','General/BC/OBC','General/BC/OBC','SC/ST','SC/ST'))
#recode neighborhood values
dat$A.A7 = mapvalues(dat$A.A7, from = c("SM00042", "SM00051", "SM00082", "SM00093"),
to = c("42", "51", "82", "93"))
dat$A.A7 = paste(dat$City, dat$A.A7)
dat$F.F1_Monthly.Income = mapvalues(dat$F.F1_Monthly.Income, from = c(-888, 888, 999),
to = c(NA, NA, NA))
##########################################################################################################################
###########################################################################################################################
#Make "graphs"
#Social
GrSoc1 = function(x){
el_bor = cbind(x$code3,as.character(x$G.G2.G2B_Code2))
el_bor2 = cbind(x$code3,as.character(x$G.G3.G3B_Code2))
el_vis = cbind(x$code3,as.character(x$I.I7.I7B_Code2))
el = rbind(el_bor, el_bor2, el_vis)
el[is.na(el[,2]),2] = el[is.na(el[,2]),1]
gr = graph_from_edgelist(el,directed=TRUE) %>% simplify(remove.multiple=FALSE, remove.loops=TRUE)
V(gr)$Religion=as.character(dat$C.C6[match(V(gr)$name,dat$code3)])
V(gr)$Caste=as.character(dat$C.C8_Caste[match(V(gr)$name,dat$code3)])
V(gr)$Neighborhood = x$A.A7[1]
return(gr)
}
#Political
GrPol1 = function(x){
el_ser = cbind(x$code3,as.character(x$I.I3_Group.I3B_Code2)) #help getting service
el_meet = cbind(x$code3,as.character(x$I.I16.I16_Lead.meeting))
el_vote = cbind(x$code3,as.character(x$I.I33B_Code2)) #important in how you vote
el_lea = cbind(x$code3,as.character(x$I.I25B_Code2)) #leader
el = rbind(el_ser, el_vote, el_lea)
el[is.na(el[,2]),2] = el[is.na(el[,2]),1] #turn non-links into loops
gr = graph_from_edgelist(el,directed=TRUE) %>% simplify(remove.multiple=FALSE, remove.loops=TRUE) #eliminate loops
V(gr)$Religion=as.character(dat$C.C6[match(V(gr)$name,dat$code3)])
V(gr)$Caste=as.character(dat$C.C8_Caste[match(V(gr)$name,dat$code3)])
V(gr)$Neighborhood = x$A.A7[1]
return(gr)
}
#Economic
GrEcon1 = function(x){
el_job = cbind(x$code3,as.character(x$D.D4.D4B_Code2)) #help getting job
el_work = cbind(x$code3,as.character(x$D.D8.D8B_Code2)) #work with
el_unemp = cbind(x$code3,as.character(x$E.E3B_Code2)) #unable to find work
el_house = cbind(x$code3,as.character(x$H.H7.H7B_Code2)) #sell house
el = rbind(el_job, el_work, el_unemp, el_house)
el[is.na(el[,2]),2] = el[is.na(el[,2]),1] #turn nonlinks into loops
gr = graph_from_edgelist(el,directed=TRUE) %>% simplify(remove.multiple=FALSE, remove.loops=TRUE) #remove loops
V(gr)$Religion=as.character(dat$C.C6[match(V(gr)$name,dat$code3)])
V(gr)$Caste=as.character(dat$C.C8_Caste[match(V(gr)$name,dat$code3)])
V(gr)$Neighborhood = x$A.A7[1]
return(gr)
}
#All Together
GrAll = function(x){
#social
el_bor = cbind(x$code3,as.character(x$G.G2.G2B_Code2))
el_bor2 = cbind(x$code3,as.character(x$G.G3.G3B_Code2))
el_vis = cbind(x$code3,as.character(x$I.I7.I7B_Code2))
#political
el_ser = cbind(x$code3,as.character(x$I.I3_Group.I3B_Code2)) #help getting service
el_meet = cbind(x$code3,as.character(x$I.I16.I16_Lead.meeting))
el_vote = cbind(x$code3,as.character(x$I.I33B_Code2)) #important in how you vote
el_lea = cbind(x$code3,as.character(x$I.I25B_Code2)) #leader
#economic
el_job = cbind(x$code3,as.character(x$D.D4.D4B_Code2)) #help getting job
el_work = cbind(x$code3,as.character(x$D.D8.D8B_Code2)) #work with
el_unemp = cbind(x$code3,as.character(x$E.E3B_Code2)) #unable to find work
el_house = cbind(x$code3,as.character(x$H.H7.H7B_Code2)) #sell house
el = rbind(el_bor, el_bor2, el_vis, el_ser, el_vote, el_lea, el_job, el_work, el_unemp, el_house)
el[is.na(el[,2]),2] = el[is.na(el[,2]),1] #turn nonlinks into loops (include isolates)
gr = graph_from_edgelist(el,directed=TRUE) %>% simplify(remove.multiple=FALSE, remove.loops=TRUE) #remove loops
V(gr)$Religion=as.character(dat$C.C6[match(V(gr)$name,dat$code3)])
V(gr)$Caste=as.character(dat$C.C8_Caste[match(V(gr)$name,dat$code3)])
V(gr)$Neighborhood = x$A.A7[1]
return(gr)
}
#create "graphs" for each set of questions
SocList1 = dlply(dat, 'A.A7', function(x) GrSoc1(x))
PolList1 = dlply(dat, 'A.A7', function(x) GrPol1(x))
EconList1 = dlply(dat, 'A.A7', function(x) GrEcon1(x))
AllList1 = dlply(dat, 'A.A7', function(x) GrAll(x))
###########################################################################################################################
###########################################################################################################################
indeg = function(x){
df = data.frame(code3 = names(V(x)),
in_degree = degree(x, mode = 'in',
loops = TRUE, normalized = FALSE))
}
outdeg = function(x){
df = data.frame(code3 = names(V(x)),
out_degree = degree(x, mode = 'out',
loops = TRUE, normalized = FALSE))
}
all_indeg = llply(AllList1, function(x) indeg(x))
all_outdeg = llply(AllList1, function(x) outdeg(x))
all_in_df = do.call(rbind, all_indeg)
all_out_df = do.call(rbind, all_outdeg)
dat$all_in = all_in_df[match(dat$code3, all_in_df$code3),'in_degree']
dat$all_out = all_out_df[match(dat$code3, all_out_df$code3),'out_degree']
dat$all_combined = dat$all_in + dat$all_out
###########################################################################################################################
###########################################################################################################################
dat$I6 = as.numeric(as.character(dat$I.I6_Community.work))
dat$I10 = mapvalues(dat$I.I10.Disagreement, from = c(888, 999), to = c(NA, NA)) %>% as.numeric(as.character())
dat$I15 = mapvalues(dat$I.I15_Community.meeting.last.12.months, from = c(888, 999), to = c(NA, NA)) %>% as.numeric(as.character())
###########################################################################################################################
###########################################################################################################################
#Table D1
a = lm('all_in ~ I6',data = dat)
b = lm('all_in ~ I6 + A.A7',data = dat)
c = lm('all_out ~ I6',data = dat)
d = lm('all_out ~ I6 + A.A7',data = dat)
e = lm('all_combined ~ I6',data = dat)
f = lm('all_combined ~ I6 + A.A7',data = dat)
stargazer(a,b,c,d,e,f,
omit = c('A.A7'),
omit.labels = c("Neighborhood dummies?"),
omit.stat = c("f", "ser"),
dep.var.labels   = c('In-degree',
'Out-degree',
'Total-degree'),
covariate.labels = c('Community Work','Constant'))
rm(a,b,c,d,e,f)
#this output must be manually edited in latex to: change dummies variable; change dep var labels
#####
#Table D2
a = lm('all_in ~ I10',data = dat)
b = lm('all_in ~ I10 + A.A7',data = dat)
c = lm('all_out ~ I10',data = dat)
d = lm('all_out ~ I10 + A.A7',data = dat)
e = lm('all_combined ~ I10',data = dat)
f = lm('all_combined ~ I10 + A.A7',data = dat)
stargazer(a,b,c,d,e,f,
omit = c('A.A7'),
omit.labels = c("Neighborhood dummies?"),
omit.stat = c("f", "ser"),
dep.var.labels   = c('In-degree',
'Out-degree',
'Total-degree'),
covariate.labels = c('Neighborhood Disagreements','Constant'))
rm(a,b,c,d,e,f)
#####
#Table D3
a = lm('all_in ~ I15',data = dat)
b = lm('all_in ~ I15 + A.A7',data = dat)
c = lm('all_out ~ I15',data = dat)
d = lm('all_out ~ I15 + A.A7',data = dat)
e = lm('all_combined ~ I15',data = dat)
f = lm('all_combined ~ I15 + A.A7',data = dat)
stargazer(a,b,c,d,e,f,
omit = c('A.A7'),
omit.labels = c("Neighborhood dummies?"),
omit.stat = c("f", "ser"),
dep.var.labels   = c('In-degree',
'Out-degree',
'Total-degree'),
covariate.labels = c('Neighborhood Meeting','Constant'))
rm(a,b,c,d,e,f)
rm(list=ls())
library(plyr);library(dplyr)
library(tidyr)
library(igraph)
library(ggplot2)
library(stargazer)
setwd('/Users/jeremyspater/Dropbox/duke/wibbels RA/Social_Density_and_Clientelism_JS-EW/Data and Code/jop replication files/scripts')
dat = readRDS('/Users/jeremyspater/Dropbox/duke/wibbels RA/segregation paper/network/3-6-17 jaipat deid.RDS')
#put caste in own category
dat$C.C8_Caste = mapvalues(dat$C.C8_Caste, from = c('BC', 'General', 'OBC', 'SC', 'ST'),
to = c('General/BC/OBC','General/BC/OBC','General/BC/OBC','SC/ST','SC/ST'))
#recode neighborhood values
dat$A.A7 = mapvalues(dat$A.A7, from = c("SM00042", "SM00051", "SM00082", "SM00093"),
to = c("42", "51", "82", "93"))
dat$A.A7 = paste(dat$City, dat$A.A7)
dat$F.F1_Monthly.Income = mapvalues(dat$F.F1_Monthly.Income, from = c(-888, 888, 999),
to = c(NA, NA, NA))
##########################################################################################################################
###########################################################################################################################
#Make "graphs"
#Social
GrSoc1 = function(x){
el_bor = cbind(x$code3,as.character(x$G.G2.G2B_Code2))
el_bor2 = cbind(x$code3,as.character(x$G.G3.G3B_Code2))
el_vis = cbind(x$code3,as.character(x$I.I7.I7B_Code2))
el = rbind(el_bor, el_bor2, el_vis)
el[is.na(el[,2]),2] = el[is.na(el[,2]),1]
gr = graph_from_edgelist(el,directed=TRUE) %>% simplify(remove.multiple=FALSE, remove.loops=TRUE)
V(gr)$Religion=as.character(dat$C.C6[match(V(gr)$name,dat$code3)])
V(gr)$Caste=as.character(dat$C.C8_Caste[match(V(gr)$name,dat$code3)])
V(gr)$Neighborhood = x$A.A7[1]
return(gr)
}
#Political
GrPol1 = function(x){
el_ser = cbind(x$code3,as.character(x$I.I3_Group.I3B_Code2)) #help getting service
el_meet = cbind(x$code3,as.character(x$I.I16.I16_Lead.meeting))
el_vote = cbind(x$code3,as.character(x$I.I33B_Code2)) #important in how you vote
el_lea = cbind(x$code3,as.character(x$I.I25B_Code2)) #leader
el = rbind(el_ser, el_vote, el_lea)
el[is.na(el[,2]),2] = el[is.na(el[,2]),1] #turn non-links into loops
gr = graph_from_edgelist(el,directed=TRUE) %>% simplify(remove.multiple=FALSE, remove.loops=TRUE) #eliminate loops
V(gr)$Religion=as.character(dat$C.C6[match(V(gr)$name,dat$code3)])
V(gr)$Caste=as.character(dat$C.C8_Caste[match(V(gr)$name,dat$code3)])
V(gr)$Neighborhood = x$A.A7[1]
return(gr)
}
#Economic
GrEcon1 = function(x){
el_job = cbind(x$code3,as.character(x$D.D4.D4B_Code2)) #help getting job
el_work = cbind(x$code3,as.character(x$D.D8.D8B_Code2)) #work with
el_unemp = cbind(x$code3,as.character(x$E.E3B_Code2)) #unable to find work
el_house = cbind(x$code3,as.character(x$H.H7.H7B_Code2)) #sell house
el = rbind(el_job, el_work, el_unemp, el_house)
el[is.na(el[,2]),2] = el[is.na(el[,2]),1] #turn nonlinks into loops
gr = graph_from_edgelist(el,directed=TRUE) %>% simplify(remove.multiple=FALSE, remove.loops=TRUE) #remove loops
V(gr)$Religion=as.character(dat$C.C6[match(V(gr)$name,dat$code3)])
V(gr)$Caste=as.character(dat$C.C8_Caste[match(V(gr)$name,dat$code3)])
V(gr)$Neighborhood = x$A.A7[1]
return(gr)
}
#All Together
GrAll = function(x){
#social
el_bor = cbind(x$code3,as.character(x$G.G2.G2B_Code2))
el_bor2 = cbind(x$code3,as.character(x$G.G3.G3B_Code2))
el_vis = cbind(x$code3,as.character(x$I.I7.I7B_Code2))
#political
el_ser = cbind(x$code3,as.character(x$I.I3_Group.I3B_Code2)) #help getting service
el_meet = cbind(x$code3,as.character(x$I.I16.I16_Lead.meeting))
el_vote = cbind(x$code3,as.character(x$I.I33B_Code2)) #important in how you vote
el_lea = cbind(x$code3,as.character(x$I.I25B_Code2)) #leader
#economic
el_job = cbind(x$code3,as.character(x$D.D4.D4B_Code2)) #help getting job
el_work = cbind(x$code3,as.character(x$D.D8.D8B_Code2)) #work with
el_unemp = cbind(x$code3,as.character(x$E.E3B_Code2)) #unable to find work
el_house = cbind(x$code3,as.character(x$H.H7.H7B_Code2)) #sell house
el = rbind(el_bor, el_bor2, el_vis, el_ser, el_vote, el_lea, el_job, el_work, el_unemp, el_house)
el[is.na(el[,2]),2] = el[is.na(el[,2]),1] #turn nonlinks into loops (include isolates)
gr = graph_from_edgelist(el,directed=TRUE) %>% simplify(remove.multiple=FALSE, remove.loops=TRUE) #remove loops
V(gr)$Religion=as.character(dat$C.C6[match(V(gr)$name,dat$code3)])
V(gr)$Caste=as.character(dat$C.C8_Caste[match(V(gr)$name,dat$code3)])
V(gr)$Neighborhood = x$A.A7[1]
return(gr)
}
#create "graphs" for each set of questions
SocList1 = dlply(dat, 'A.A7', function(x) GrSoc1(x))
PolList1 = dlply(dat, 'A.A7', function(x) GrPol1(x))
EconList1 = dlply(dat, 'A.A7', function(x) GrEcon1(x))
AllList1 = dlply(dat, 'A.A7', function(x) GrAll(x))
###########################################################################################################################
###########################################################################################################################
indeg = function(x){
df = data.frame(code3 = names(V(x)),
in_degree = degree(x, mode = 'in',
loops = TRUE, normalized = FALSE))
}
outdeg = function(x){
df = data.frame(code3 = names(V(x)),
out_degree = degree(x, mode = 'out',
loops = TRUE, normalized = FALSE))
}
all_indeg = llply(AllList1, function(x) indeg(x))
all_outdeg = llply(AllList1, function(x) outdeg(x))
all_in_df = do.call(rbind, all_indeg)
all_out_df = do.call(rbind, all_outdeg)
dat$all_in = all_in_df[match(dat$code3, all_in_df$code3),'in_degree']
dat$all_out = all_out_df[match(dat$code3, all_out_df$code3),'out_degree']
dat$all_combined = dat$all_in + dat$all_out
###########################################################################################################################
###########################################################################################################################
dat$I6 = as.numeric(as.character(dat$I.I6_Community.work))
dat$I10 = mapvalues(dat$I.I10.Disagreement, from = c(888, 999), to = c(NA, NA)) %>% as.numeric(as.character())
dat$I15 = mapvalues(dat$I.I15_Community.meeting.last.12.months, from = c(888, 999), to = c(NA, NA)) %>% as.numeric(as.character())
###########################################################################################################################
###########################################################################################################################
#Table D1
a = lm('all_in ~ I6',data = dat)
b = lm('all_in ~ I6 + A.A7',data = dat)
c = lm('all_out ~ I6',data = dat)
d = lm('all_out ~ I6 + A.A7',data = dat)
e = lm('all_combined ~ I6',data = dat)
f = lm('all_combined ~ I6 + A.A7',data = dat)
stargazer(a,b,c,d,e,f,
omit = c('A.A7'),
omit.labels = c("Neighborhood dummies?"),
omit.stat = c("f", "ser"),
dep.var.labels   = c('In-degree',
'Out-degree',
'Total-degree'),
covariate.labels = c('Community Work','Constant'))
rm(a,b,c,d,e,f)
#this output must be manually edited in latex to: change dummies variable; change dep var labels
#####
#Table D2
a = lm('all_in ~ I10',data = dat)
b = lm('all_in ~ I10 + A.A7',data = dat)
c = lm('all_out ~ I10',data = dat)
d = lm('all_out ~ I10 + A.A7',data = dat)
e = lm('all_combined ~ I10',data = dat)
f = lm('all_combined ~ I10 + A.A7',data = dat)
stargazer(a,b,c,d,e,f,
omit = c('A.A7'),
omit.labels = c("Neighborhood dummies?"),
omit.stat = c("f", "ser"),
dep.var.labels   = c('In-degree',
'Out-degree',
'Total-degree'),
covariate.labels = c('Neighborhood Disagreements','Constant'))
rm(a,b,c,d,e,f)
#####
#Table D3
a = lm('all_in ~ I15',data = dat)
b = lm('all_in ~ I15 + A.A7',data = dat)
c = lm('all_out ~ I15',data = dat)
d = lm('all_out ~ I15 + A.A7',data = dat)
e = lm('all_combined ~ I15',data = dat)
f = lm('all_combined ~ I15 + A.A7',data = dat)
stargazer(a,b,c,d,e,f,
omit = c('A.A7'),
omit.labels = c("Neighborhood dummies?"),
omit.stat = c("f", "ser"),
dep.var.labels   = c('In-degree',
'Out-degree',
'Total-degree'),
covariate.labels = c('Neighborhood Meeting','Constant'))
rm(a,b,c,d,e,f)
#set working directory
#setwd('')
A = read.csv('../data/6-27-17_supermerge_wleaderfrac_deid_trimmed.csv', sep = ';')
s = function(x){summary(factor(x))}
s(A$Evict)
